package com.drama.api.sys.role.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.drama.api.base.Constants;
import com.drama.api.base.Type;
import com.drama.api.sys.log.dao.SysLogDao;
import com.drama.api.sys.role.dao.SysRoleAuthorityDao;
import com.drama.api.sys.role.dao.SysRoleDao;
import com.drama.api.sys.role.dao.SysUserRoleDao;
import com.drama.api.sys.role.domain.po.SysRoleAuthorityPO;
import com.drama.api.sys.role.domain.po.SysRolePO;
import com.drama.api.sys.role.domain.po.SysUserRolePO;
import com.drama.api.sys.role.domain.vo.SysRoleAddVO;
import com.drama.api.sys.role.domain.vo.SysRoleSearchVO;
import com.drama.api.sys.role.domain.vo.SysRoleUpdateVO;
import com.drama.api.sys.role.domain.vo.SysRoleVO;
import com.drama.api.sys.role.service.ISysRoleService;
import com.drama.api.task.SysLogThread;
import com.drama.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;


@Service
@Transactional
@Slf4j
public class SysRoleServiceImpl implements ISysRoleService {

    @Autowired
    private SysRoleDao sysRoleDao;
    @Autowired
    private SysRoleAuthorityDao sysRoleAuthorityDao;
    @Autowired
    private SysUserRoleDao sysUserRoleDao;
    @Autowired
    private SysLogDao sysLogDao;


    @Override
    public List<SysRoleVO> getList(SysRoleSearchVO searchVO) {
        return sysRoleDao.getList(searchVO);
    }

    @Override
    public void add(SysRoleAddVO vo) {
        SysRolePO po = new SysRolePO();
        BeanUtils.copyProperties(vo, po);
        po.preInsert();
        po.setRoleType(2);
        addRoleAuthority(po, vo.getAuthorityIds());
        int i = sysRoleDao.insertSelective(po);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
        new SysLogThread(sysLogDao,Constants.SYS_LOG_MODULE[4],"添加角色信息，角色ID:"+po.getId(), (String) StpUtil.getLoginId()).start();
    }


    @Override
    public void delete(String id) {
        if (StringUtils.isBlank(id)) {
            throw new BusinessException(Type.PARAM_VALIDATE_FAIL);
        }
        SysRolePO sysRolePO=sysRoleDao.selectByPrimaryKey(id);
        if (sysRolePO==null) {
            throw new BusinessException(Type.NOT_FOUND_ERROR);
        }
        if(sysRolePO.getRoleType()==1){
            throw new BusinessException(Type.FAIL.getCode(),  "系统角色不允许删除！");
        }
        Example example = new Example(SysUserRolePO.class);
        example.and().andEqualTo("roleId", id);
        List<SysUserRolePO> list = sysUserRoleDao.selectByExample(example);
        if (!list.isEmpty()) {
            throw new BusinessException(Type.FAIL.getCode(),  "当前角色有关联用户,不能删除！");
        }
        Example example1 = new Example(SysRoleAuthorityPO.class);
        example1.and().andEqualTo("roleId", id);
        sysRoleAuthorityDao.deleteByExample(example1);
        int i = sysRoleDao.deleteByPrimaryKey(id);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
        new SysLogThread(sysLogDao,Constants.SYS_LOG_MODULE[4],"删除角色，角色ID:"+sysRolePO.getId(), (String) StpUtil.getLoginId()).start();
    }

    @Override
    public void update(SysRoleUpdateVO vo) {
        SysRolePO po = new SysRolePO();
        BeanUtils.copyProperties(vo, po);
        po.preUpdate();
        Example example = new Example(SysRoleAuthorityPO.class);
        example.and().andEqualTo("roleId", po.getId());
        sysRoleAuthorityDao.deleteByExample(example);
        addRoleAuthority(po, vo.getAuthorityIds());
        int i = sysRoleDao.updateByPrimaryKeySelective(po);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
        new SysLogThread(sysLogDao,Constants.SYS_LOG_MODULE[4],"更新角色信息，角色ID:"+po.getId(), (String) StpUtil.getLoginId()).start();
    }

    /**
     * 添加用户权限
     * @param po
     * @param authorityIds
     */
    private void addRoleAuthority(SysRolePO po, List<String> authorityIds) {
        if (authorityIds != null && authorityIds.size() > 0) {
            for (String authorityId : authorityIds) {
                SysRoleAuthorityPO sysRoleAuthorityPO = new SysRoleAuthorityPO();
                sysRoleAuthorityPO.setRoleId(po.getId());
                sysRoleAuthorityPO.setAuthorityId(authorityId);
                sysRoleAuthorityDao.insertSelective(sysRoleAuthorityPO);
            }
        }
    }
}